{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "73891da3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#使用人脸检测API方法和人脸识别相同\n",
    "#引入requests模块\n",
    "import requests\n",
    "#设置API URL\n",
    "baseurl = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "#设置通行证\n",
    "apikey = 'vGlwjQJXYKWyXOELKQFxrFFQzD88d4Cx'\n",
    "apisec = 'rKDegTysiZpO47UF-Y_-bMwxN8PygYEN'\n",
    "\n",
    "#准备payload\n",
    "payload={\n",
    "    'api_key':apikey,\n",
    "    'api_secret':apisec,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'\n",
    "}\n",
    "\n",
    "#发送请求\n",
    "r = requests.post(url = baseurl, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "82ba0a04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6d7f5187",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647966258,a405b4d7-c5d9-46e4-a879-94b20e350fde',\n",
       " 'time_used': 609,\n",
       " 'faces': [{'face_token': '5ccd3acc44c67bfc4f7afecbc3f36980',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4c3c6f6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'5ccd3acc44c67bfc4f7afecbc3f36980'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用字典方法提取faces中的face_token（人脸对比需要使用）\n",
    "face_token01 = r.json()['faces'][0]['face_token']\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7357d76e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#获取第二张人脸token\n",
    "#设置API URL\n",
    "baseurl = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "#设置通行证\n",
    "apikey = 'vGlwjQJXYKWyXOELKQFxrFFQzD88d4Cx'\n",
    "apisec = 'rKDegTysiZpO47UF-Y_-bMwxN8PygYEN'\n",
    "\n",
    "#准备payload\n",
    "payload={\n",
    "    'api_key':apikey,\n",
    "    'api_secret':apisec,\n",
    "    'image_url':'https://img1.baidu.com/it/u=73274807,795212706&fm=253&fmt=auto&app=138&f=JPEG?w=298&h=399'\n",
    "}\n",
    "\n",
    "#发送请求\n",
    "r = requests.post(url = baseurl, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4220c057",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6722e13c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'36d2a2f1bd6ae83d4667b81d347d0c0c'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "94a3b78f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#利用两个token进行人脸识别\n",
    "#设置API URL\n",
    "baseurl = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "#设置通行证\n",
    "apikey = 'vGlwjQJXYKWyXOELKQFxrFFQzD88d4Cx'\n",
    "apisec = 'rKDegTysiZpO47UF-Y_-bMwxN8PygYEN'\n",
    "\n",
    "#准备payload\n",
    "payload={\n",
    "    'api_key':apikey,\n",
    "    'api_secret':apisec,\n",
    "    'face_token1':face_token01,\n",
    "    'face_token2':face_token02\n",
    "}\n",
    "\n",
    "#发送请求\n",
    "r = requests.post(url = baseurl, params = payload)\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "77fb924c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647966641,fa313424-d605-40af-8558-8b9df6e144df',\n",
       " 'time_used': 279,\n",
       " 'confidence': 97.389,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取结果\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3c583030",
   "metadata": {},
   "outputs": [],
   "source": [
    "#其他知识点：封装人脸检测函数，实现快速调用\n",
    "API_key = 'vGlwjQJXYKWyXOELKQFxrFFQzD88d4Cx'\n",
    "API_sercret = 'rKDegTysiZpO47UF-Y_-bMwxN8PygYEN'\n",
    "img_url_01 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "img_url_02 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=2'\n",
    "#用def函数编写模块\n",
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "42de1f45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647966749,7e1b87af-e60f-4a04-bf15-c314a004a441',\n",
       " 'time_used': 727,\n",
       " 'faces': [{'face_token': '676ba80f5aa96127a67d6a7b39fbe638',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "27a19fee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647966761,606a9c32-9fee-468b-9710-8abe4e52992c',\n",
       " 'time_used': 317,\n",
       " 'faces': [{'face_token': '933cf4c6976b296c0ec3b47c5ebda8b0',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "475e413d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
